package com.binvshe.binvshe.message.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.contacts.SearchResultAdapter;
import com.binvshe.binvshe.message.dao.Message;
import com.binvshe.binvshe.message.dao.MessageDao;
import com.binvshe.binvshe.message.dao.MessageHelper;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.GlideHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 好友申请消息
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-10-28
 * @Version: 1.0.0
 */
public class FriendMsgListAdapter extends SearchResultAdapter {

    public FriendMsgListAdapter(Context con) {
        super(con);
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        final Message msg = (Message)getData().get(position);

        viewHolder.tvName.setText(msg.getTitle());
        GlideHelper.loadImageWithRound(getContext(), viewHolder.ivPortraid, msg.getIcon());

        String extra = msg.getExtra();
        try {
            JSONObject jobj = new JSONObject(extra);
            final String request_id = jobj.optString("request_id");
            String reason = jobj.optString("reason");
            viewHolder.tvContent.setText(reason);

            String state = jobj.optString("state");
            // 如果为空的话， 就默认还没已经同意请求了
            viewHolder.button.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(state)) {
                viewHolder.button.setText("接受");
                viewHolder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        acceptFriendAddition(msg, request_id);
                    }
                });
            } else {
                viewHolder.button.setText("已接受");
                viewHolder.button.setEnabled(false);
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    void acceptFriendAddition(final Message msg, String requestId) {
        String url = String.format(UrlCenter.AGREE_FRIEND_REQEST, requestId);
        IHttpRequest request = new MyHttpRequest(getContext());
        request.put(url, null, new DataRequestListener<Object>(Object.class) {

            @Override
            public void onSuccess(Object obj) {
                super.onSuccess(obj);
                EasyToast.showToast(getContext(), "接受请求成功");
                udpateState(msg);
                getData().clear();
                /*if (getContext() instanceof FriendsMsgListActivity) {
                    FriendsMsgListActivity act = (FriendsMsgListActivity)getContext();
                    act.startLoading();
                }*/

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(getContext(), strMsg);
            }
        });
    }

    private void udpateState(Message msg) {
        try {
            JSONObject extraObj = new JSONObject(msg.getExtra());
            extraObj.put("state", 1);
            msg.setExtra(extraObj.toString());
            MessageDao dao = MessageHelper.getInstance().getMsgDao();
            dao.insertOrReplace(msg);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
